package main

import "fmt"

type ListNode struct {
    Val int
    Next *ListNode
}

func reverseList(head *ListNode) *ListNode {
    var prev *ListNode
    curr := head
    for curr != nil {
        next := curr.Next
        curr.Next = prev
        prev = curr
        curr = next
    }
    return prev
}

func main() {
    // 创建链表
    node1 := &ListNode{Val: 1}
    node2 := &ListNode{Val: 2}
    node3 := &ListNode{Val: 3}
    node4 := &ListNode{Val: 4}
    node5 := &ListNode{Val: 5}
    node1.Next = node2
    node2.Next = node3
    node3.Next = node4
    node4.Next = node5

    fmt.Println("原始链表：")
    printList(node1)

    reversedList := reverseList(node1)

    fmt.Println("反转后的链表：")
    printList(reversedList)
}

func printList(head *ListNode) {
    curr := head
    for curr != nil {
        fmt.Printf("%d ", curr.Val)
        curr = curr.Next
    }
    fmt.Println()
}